﻿using System;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using LTJ.Entity;
using LTJ.Common;
using LTJ.Common.Generic;
using LTJ.Config;
using LTJ.FrameWork;

namespace uswinweb.user
{
    /// <summary>
    /// 用户后台管理模板页
    /// </summary>
    public partial class usermain : System.Web.UI.MasterPage
    {
        protected DataTable maindt = new DataTable();
        protected DataTable subdt = new DataTable();
        protected CompanyInfo companyinfo = new CompanyInfo();
        protected List<Authority> authlist = new List<Authority>();
        public string pagename = "";
        protected string currentParentLink = "";
        protected string currentMainmenu = "";
        protected string currentSubmenu = "";
        protected GeneralConfigInfo config = GeneralConfigs.GetConfig();
        protected string authslist = "";
        protected string username = "";
        protected int compid = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            username = FrameWorkUtil.GetCookie("username");
            compid = Utils.StrToInt(FrameWorkUtil.GetCookie("usercid"), 0);
            maindt = Caches.GetUserMenu(LTJ.Entity.UserMenuType.MainMenu);
            subdt = Caches.GetUserMenu(LTJ.Entity.UserMenuType.SubMenu);
            pagename = SASRequest.GetPageName();
            companyinfo = Companys.GetCompanyByID(compid);
            authlist = Users.GetAuthListByUid(Utils.StrToInt(FrameWorkUtil.GetCookie("userid"), 0));
            LTJ.Entity.Authority authinfo = Users.GetAuthInfoCache(compid, Utils.StrToInt(FrameWorkUtil.GetCookie("userid"), 0));
            authslist = authinfo.Authoritys;

            if (pagename != "index.aspx")
            {
                string currentParentid = "";
                currentSubmenu = subdt.Select("menulink = '" + pagename + "'").Length > 0 ? subdt.Select("menulink = '" + pagename + "'")[0]["menutitle"].ToString() : "";
                currentParentid = subdt.Select("menulink = '" + pagename + "'").Length > 0 ? subdt.Select("menulink = '" + pagename + "'")[0]["parentid"].ToString() : "";
                currentParentLink = subdt.Select("parentid = '" + currentParentid + "'").Length > 0 ? subdt.Select("parentid = '" + currentParentid + "'")[0]["menulink"].ToString() : "";

                if (currentParentid != "")
                {
                    currentMainmenu = maindt.Select("menuid = '" + currentParentid + "'").Length > 0 ? maindt.Select("menuid = '" + currentParentid + "'")[0]["menutitle"].ToString() : "";
                }
            }

            this.Page.Title = currentMainmenu + " - " + currentSubmenu;
        }

        /// <summary>
        /// 权限限制菜单读取
        /// </summary>
        protected string GetMemnuWithAuth()
        {
            int count = 1;
            StringBuilder sb = new StringBuilder();

            foreach (System.Data.DataRow dr in maindt.Rows)
            {
                string tempstr = "";
                foreach (System.Data.DataRow drr in subdt.Select("parentid = " + dr["menuid"]))
                {
                    if (!("," + companyinfo.Authoritys).Contains("," + drr["menulink"] + "|")) continue;
                    if (!("," + authslist).Contains("," + drr["menulink"] + "|")) continue;
                    tempstr += "<span><a href=\"" + drr["menulink"] + "\">" + drr["menutitle"] + "</a></span>";
                }

                if (tempstr != "")
                {

                    sb.Append("<li class=\"li1\">");
                    sb.AppendFormat("<p class=\"zi\">{0}</p>", dr["menutitle"]);
                    sb.Append("<p class=\"c\">");
                    sb.Append(tempstr);
                    sb.Append("</p>");
                    sb.Append("</li>");

                    if (count < maindt.Rows.Count)
                    {
                        sb.Append("<li class=\"li2\"></li>");
                    }
                }

                count++;
            }

            return sb.ToString();
        }
    }
}